package com.afkt.project.ui.slice;

import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.model.bean.ItemBean;
import com.afkt.project.ui.adapter.PagerSnapMAXAdapter;
import com.ohos.afkt.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;


import java.util.ArrayList;
import java.util.List;

public class PagerSnapMAXActivity extends BaseActivity implements Component.ScrolledListener {


    private ListContainer vidBvrRecy;

    private DirectionalLayout bar_Layout;
    private DirectionalLayout bar;
    private Image select_retuen;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        bar_Layout = (DirectionalLayout) findComponentById(ResourceTable.Id_base_view_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_sheet_dialog_bar, null, false);
        Text text = (Text) bar.findComponentById(ResourceTable.Id_titleText);
        select_retuen = (Image) bar.findComponentById(ResourceTable.Id_sheet_dialog_return);
        text.setText("LinearSnapHelper-RecyclerView");
        bar_Layout.addComponent(bar);
        vidBvrRecy = (ListContainer) findComponentById(ResourceTable.Id_vid_bvr_recy);
        initValue();
    }

    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_activity_page_snap_max;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void onBackground() {
        super.onBackground();
        bar_Layout.removeAllComponents();
    }

    @Override
    protected void initListener() {
        select_retuen.setClickedListener(component -> {
            // LogUtils.error(TAG, "return 点击");
            onBackPressed();
        });
    }

    private PagerSnapMAXAdapter pagerSnapAdapter;
    private void initValue() {
        List<ItemBean> lists = new ArrayList<>();
        for (int i = 0; i <= 9; i++) {
            lists.add(ItemBean.newItemBeanPager());
        }
        pagerSnapAdapter = new PagerSnapMAXAdapter(lists, this);
        vidBvrRecy.setItemProvider(pagerSnapAdapter);
        int size = lists.size();
        //ListViewUtils.smoothScrollToPosition(vidBvrRecy, size * 100 + 1);
        vidBvrRecy.setCentralScrollMode(true);
        vidBvrRecy.scrollToCenter(2);
    }

    private class MyEventHandler extends EventHandler {

    public MyEventHandler(EventRunner runner) throws IllegalArgumentException {
        super(runner);
    }

    @Override
    protected void processEvent(InnerEvent event) {
        super.processEvent(event);
        if (event == null) {
            return;
        }
        int eventId = event.eventId;
        Object list = event.object;
        if (list instanceof  ListContainer) {
            ListContainer listContainer = (ListContainer) list;
            int lastItemPosition = listContainer.getItemPosByVisibleIndex(listContainer.getVisibleIndexCount() - 1);
            int firstItemPosition = listContainer.getItemPosByVisibleIndex(0);
            int currentPosition = (lastItemPosition + firstItemPosition) / 2;
            int index = pagerSnapAdapter.getRealIndex(currentPosition);
        }
        switch (eventId) {
            case 1: {

            }
        }
    }
}
    private EventHandler eventHandler = new EventHandler(EventRunner.create());


    @Override
    public void onContentScrolled(Component component, int i, int i1, int i2, int i3) {
        //eventHandler.sendEvent();
    }
}
